package com.fu.shop.controller;

import com.fu.core.common.JsonResult;
import com.fu.shop.service.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

import java.util.List;
import java.util.Map;

/**
 * @author 付龙江
 * @date 2022/1/29 0:14
 */
@RestController
@CrossOrigin
@RequestMapping("/userManager")
@Api(tags = "后台用户管理")
public class UserController {

    UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping(value = "/userList")
    @ApiOperation(value = "顾客管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "顾客名称", dataType = "String", required = false),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "size", value = "每页条数", dataType = "Integer", required = true)
    })
    public Map<String,Object> selectUserList(String userName, Integer page, Integer size){
        System.out.println(userName+"   "+page+"   "+size);
        PageInfo<Map<String, Object>> mapPageInfo = userService.selectUserList(userName, page, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("total",mapPageInfo.getTotal());
        jsonResult.setField("users",mapPageInfo.getList());
        return jsonResult.getMap();
    }

    @PostMapping("/uploadUserList")
    @ApiOperation(value = "导出用户信息")
    public void uploadUserList(HttpServletResponse response)throws IOException {
        List<Map<String, Object>> list = userService.uploadUserList();
        //导出
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("用户信息表");
        String fileName="用户信息表"+".xls";
        fileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");
        String[] headers = {"顾客编号", "姓名", "电话", "所在城市","注册时间", "历史消费金额"};
        // 创建一个行对象, 作为表头
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            // 在行中添加单元格
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            // 把字段名添加到表头行上
            cell.setCellValue(text);
        }
        int rowNum = 1;
        for (Map<String, Object> map : list) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(map.get("userId").toString());
            dataRow.createCell(1).setCellValue(map.get("userName").toString());
            dataRow.createCell(2).setCellValue(map.get("userPhone").toString());
            dataRow.createCell(3).setCellValue(map.get("userCity").toString());
            dataRow.createCell(4).setCellValue(map.get("userReg").toString());
            dataRow.createCell(5).setCellValue(map.get("money").toString());
            rowNum++;
        }
        //返回给前端下载
        response.setContentType("application/octet-stream");
        //附件格式打卡，不是打开页面
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }
}
